#include "TestThreadSafeQueue.h"

#include <stdio.h>

ThreadSafeQueue<int> TestThreadSafeQueue::_queue;
boost::mutex TestThreadSafeQueue::_ioMutex;

void TestThreadSafeQueue::print(char *string, int element)
{
	boost::mutex::scoped_lock(ioMutex);
	printf("%s%d\n", string, element);
	boost::posix_time::milliseconds waitTime(10);
	boost::this_thread::sleep(waitTime);
}

void TestThreadSafeQueue::threadMain()
{
	while (true)
	{
		int element = _queue.get();
		print("read: ", element);
		boost::posix_time::milliseconds waitTime(rand() * 900 / RAND_MAX);
		boost::this_thread::sleep(waitTime);
	}
	return;
}

void TestThreadSafeQueue::run()
{
	boost::thread thread = boost::thread(boost::bind(&threadMain));

	while (true)
	{
		int element = rand();
		_queue.append(element);
		print("written: ", element);
		boost::posix_time::milliseconds waitTime(500);
		boost::this_thread::sleep(waitTime);
	}

	return;
}
